package android.slc.module.component.resolve;

import java.util.ArrayList;
import java.util.List;

public class ResolveByteBuf implements IByteBuf<ResolveByteBuf> {
    private static final boolean checkBounds = true;
    private final List<Byte> cacheByteBuf = new ArrayList<>();
    private int markedReaderIndex = 0;
    private int readerIndex = 0;

    public ResolveByteBuf() {
    }

    public ResolveByteBuf(byte[] byteArray) {
        for (byte value : byteArray) {
            cacheByteBuf.add(value);
        }
    }

    public ResolveByteBuf(ResolveByteBuf src) {
        cacheByteBuf.addAll(src.cacheByteBuf);
    }

    /**
     * 可读取字节数
     *
     * @return
     */
    @Override
    public int readableBytes() {
        return cacheByteBuf.size();
    }

    /////////////////////写入////////////////////////

    /**
     * 写入字节
     *
     * @param value
     * @return
     */
    @Override
    public ResolveByteBuf writeByte(byte value) {
        cacheByteBuf.add(value);
        return this;
    }

    /**
     * 写入字节
     *
     * @param byteArray
     * @return
     */
    @Override
    public ResolveByteBuf writeBytes(byte[] byteArray) {
        for (byte value : byteArray) {
            cacheByteBuf.add(value);
        }
        return this;
    }

    /**
     * 写入字节
     *
     * @param src
     * @return
     */
    @Override
    public ResolveByteBuf writeBytes(ResolveByteBuf src) {
        cacheByteBuf.addAll(src.cacheByteBuf);
        return this;
    }

    /////////////////////读取////////////////////////

    @Override
    public int getReaderIndex() {
        return readerIndex;
    }

    @Override
    public ResolveByteBuf markReaderIndex() {
        markedReaderIndex = readerIndex;
        return this;
    }

    @Override
    public ResolveByteBuf resetReaderIndex() {
        readerIndex(markedReaderIndex);
        return this;
    }


    @Override
    public ResolveByteBuf readerIndex(int readerIndex) {
        if (checkBounds) {
            checkIndexBounds(readerIndex);
        }
        this.readerIndex = readerIndex;
        return this;
    }

    /**
     * 读取这个字节的位置
     *
     * @param value
     * @return
     */
    @Override
    public int bytesBefore(byte value) {
        return cacheByteBuf.indexOf(value);
    }

    /**
     * 读取
     *
     * @return
     */
    @Override
    public byte readByte() {
        if (checkBounds) {
            checkIndexBounds(readerIndex);
        }
        byte value = cacheByteBuf.get(readerIndex);
        readerIndex++;
        return value;
    }

    private void checkIndexBounds(final int index) {
        if (index < 0 || index >= cacheByteBuf.size()) {
            throw new IndexOutOfBoundsException("readerIndex 越界");
        }
    }

    @Override
    public ResolveByteBuf discardReadBytes() {
        if (readerIndex == 0) {
            return this;
        }
        if (readerIndex >= cacheByteBuf.size()) {
            cacheByteBuf.clear();
        }else{
            List<Byte> newCacheByteBuf = new ArrayList<>(cacheByteBuf.subList(readerIndex + 1, cacheByteBuf.size()));
            cacheByteBuf.clear();
            cacheByteBuf.addAll(newCacheByteBuf);
        }
        readerIndex = 0;
        markReaderIndex();
        return this;
    }

}
